//
//  LoginViewController.m
//  NewProject
//
//  Created by 王嘉琪 on 2017/6/6.
//  Copyright © 2017年 李航飞. All rights reserved.
//

#import "LoginViewController.h"
#import "ZhuCeViewController.h"
#import "DingWeiViewController.h"

@interface LoginViewController ()<UITextViewDelegate,UIAlertViewDelegate>
@property(nonatomic,strong)UITextField* telTextField;
@property(nonatomic,strong)UITextField* passWordField;
@property(nonatomic,strong)UIButton* jizhumima;
@property(nonatomic,strong)NSString* jizhu;
@property(nonatomic,strong)UIImageView* duihaoImg;
@end

@implementation LoginViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpView];
    [self getJson];
    // Do any additional setup after loading the view.
}
-(void)getJson{
    
}
- (void)setUpView{
    self.title=@"登录";
    _jizhu=@"0";

    self.view.backgroundColor=[UIColor whiteColor];
    
    _telTextField =[[UITextField alloc] initWithFrame:CGRectMake(15*kScrenWidth, 25*kScrenHidth+64, self.view.frame.size.width-15*kScrenWidth*2, 50*kScrenHidth)];
    _telTextField.backgroundColor=[UIColor whiteColor];
    _telTextField.delegate=self;
    _telTextField.clearButtonMode=YES;
    [_telTextField setFont:[UIFont systemFontOfSize:14]];
    _telTextField.placeholder = @"请输入手机号码";
    _telTextField.keyboardType=UIKeyboardTypePhonePad;
    UIImageView *imageViewPwd=[[UIImageView alloc]initWithFrame:CGRectMake(10*kScrenWidth, 10*kScrenWidth, 50*kScrenWidth, 50*kScrenWidth)];
    imageViewPwd.image=[UIImage imageNamed:@"deng_03.gif"];
    _telTextField.leftView=imageViewPwd;
    _telTextField.leftViewMode=UITextFieldViewModeAlways; //此处用来设置leftview现实时机
    _telTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _telTextField.layer.borderColor=[UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1].CGColor;
    _telTextField.layer.borderWidth= 1.0f;
    _telTextField.layer.cornerRadius = 6.0f;
    [self.view addSubview:_telTextField];
    
    
    _passWordField =[[UITextField alloc] initWithFrame:CGRectMake(15*kScrenWidth, _telTextField.frame.size.height+_telTextField.frame.origin.y+25*kScrenHidth, self.view.frame.size.width-15*kScrenWidth*2, 50*kScrenHidth)];
    _passWordField.backgroundColor=[UIColor whiteColor];
    _passWordField.delegate=self;
    _passWordField.secureTextEntry=YES;
    _passWordField.clearButtonMode=YES;
    [_passWordField setFont:[UIFont systemFontOfSize:14]];
    _passWordField.placeholder = @"请输入密码";
    UIImageView *imageViewPwd1=[[UIImageView alloc]initWithFrame:CGRectMake(10*kScrenWidth, 10*kScrenWidth, 30*kScrenWidth, 30*kScrenWidth)];
    imageViewPwd1.image=[UIImage imageNamed:@"deng_06.gif"];
    _passWordField.leftView=imageViewPwd1;
    _passWordField.leftView.frame=CGRectMake(0, 0, 50, 50);
    _passWordField.leftViewMode=UITextFieldViewModeAlways; //此处用来设置leftview现实时机
    _passWordField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _passWordField.layer.borderColor=[UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1].CGColor;
    _passWordField.layer.borderWidth= 1.0f;
    _passWordField.layer.cornerRadius = 6.0f;
    [self.view addSubview:_passWordField];
    
    
    UIButton *jizhumima=[[UIButton alloc] initWithFrame:CGRectMake(15*kScrenWidth, _passWordField.frame.size.height+_passWordField.frame.origin.y+30*kScrenHidth, 20*kScrenWidth, 20*kScrenWidth)];
    _duihaoImg=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20*kScrenWidth, 20*kScrenWidth)];
    [_duihaoImg setImage:[UIImage imageNamed:@"没选"]];
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(_duihaoImg.frame.size.width+_duihaoImg.frame.origin.x, 0, jizhumima.frame.size.width-_duihaoImg.frame.size.width-_duihaoImg.frame.origin.x, 30*kScrenHidth)];
    [jizhumima addSubview:view];
    [jizhumima addSubview:_duihaoImg];
    [jizhumima addTarget:self action:@selector(jizhumimaBtn) forControlEvents:UIControlEventTouchUpInside];
    _jizhumima=jizhumima;
    [self.view addSubview:jizhumima];
    
    UIButton *jizhumima1=[[UIButton alloc] initWithFrame:CGRectMake(jizhumima.frame.origin.x+jizhumima.frame.size.width, jizhumima.frame.origin.y-5, 120*kScrenWidth, 30*kScrenHidth)];
    [jizhumima1 setTitle:@" 记住密码" forState:UIControlStateNormal];
    [jizhumima1 addTarget:self action:@selector(jizhumimaBtn) forControlEvents:UIControlEventTouchUpInside];
    [jizhumima1.titleLabel setFont:[UIFont systemFontOfSize:14]];
    jizhumima1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [jizhumima1 setTitleColor:[UIColor colorWithRed:201/255.0 green:200/255.0 blue:200/255.0 alpha:1] forState:UIControlStateNormal];
    [self.view addSubview:jizhumima1];
    
    UIButton *wangjimima=[[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/4*3-30, jizhumima1.frame.origin.y, self.view.frame.size.width/4, 30)];
    wangjimima.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [wangjimima.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [wangjimima setTitle:@"忘记密码？" forState:UIControlStateNormal];
    [wangjimima setTitleColor:[UIColor colorWithRed:201/255.0 green:200/255.0 blue:200/255.0 alpha:1] forState:UIControlStateNormal];
    [self.view addSubview:wangjimima];
    
    //登录按钮
    UIButton *login=[[UIButton alloc] initWithFrame:CGRectMake(15*kScrenWidth, wangjimima.frame.size.height+wangjimima.frame.origin.y+40*kScrenHidth, self.view.frame.size.width-15*kScrenWidth*2, 40*kScrenHidth)];
    //按钮设置圆角
    login.clipsToBounds=YES;
    login.layer.cornerRadius=8;
    [login setTitle:@"登录" forState:UIControlStateNormal];
    [login setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [login.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [login addTarget:self action:@selector(dengLu) forControlEvents:UIControlEventTouchUpInside];
    login.backgroundColor=[UIColor colorWithRed:255/255.0 green:214/255.0 blue:1/255.0 alpha:1];
    [self.view addSubview:login];
    
    //注册
    UIButton *zhuce=[[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/4*3-30, login.frame.origin.y+login.frame.size.height+20, self.view.frame.size.width/4, 30)];
     zhuce.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [zhuce.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [zhuce setTitle:@"立即注册" forState:UIControlStateNormal];
    [zhuce addTarget:self action:@selector(gotoZhuCe) forControlEvents:UIControlEventTouchUpInside];
    [zhuce setTitleColor:[UIColor colorWithRed:255/255.0 green:214/255.0 blue:1/255.0 alpha:1] forState:UIControlStateNormal];
    [self.view addSubview:zhuce];
}
- (void)jizhumimaBtn{
    if([_jizhu isEqual:@"0"]){
        [_duihaoImg setImage:[UIImage imageNamed:@"选中1"]];
        [_jizhumima addSubview:_duihaoImg];
        _jizhu=@"1";
    }else{
        [_duihaoImg setImage:[UIImage imageNamed:@"没选"]];
        [_jizhumima addSubview:_duihaoImg];
        _jizhu=@"0";
    }
}
- (void)gotoZhuCe{
    ZhuCeViewController *zhuceVC=[[ZhuCeViewController alloc] init];
    [self.navigationController pushViewController:zhuceVC animated:YES];
}
- (void)dengLu{
    
    
    if([_telTextField.text isEqualToString:@""]){
        UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@""
                                                       message:@"请输入手机号码" delegate:self
                                             cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    }else if([_passWordField.text isEqualToString:@""]){
        UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@""
                                                       message:@"请输入密码" delegate:self
                                             cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    }else{
        AFHTTPSessionManager*manager=[AFHTTPSessionManager manager];
        manager.responseSerializer.acceptableContentTypes = nil;
        manager.requestSerializer=[AFHTTPRequestSerializer serializer];
        manager.responseSerializer=[AFHTTPResponseSerializer serializer];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        manager.responseSerializer.acceptableContentTypes =  [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/javascript",@"text/plain", nil];
        
        NSString*urlstr=[NSString stringWithFormat:@"http://www.lnwuyou.com/sc/index.php?g=portal&m=index&a=login_buyer&tel=%@&pass=%@",_telTextField.text,_passWordField.text];
        NSDictionary * paramDic = @{@"tel":_telTextField.text,@"pass":_passWordField.text};
        [manager GET:urlstr parameters:nil  progress:nil success:^(NSURLSessionDataTask *operation, id responseObject) {
            NSLog(@"111%@",responseObject);
            NSDictionary*dic=[[NSDictionary alloc]init];
            dic=responseObject;
            if([dic[@"status"] isEqualToString:@"1"]){
                NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
                NSString *myId=dic[@"id"];
                [user setObject:myId forKey:@"myId"];
                UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@""
                                                               message:@"登录成功" delegate:self
                                                     cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                alert.tag=100;
                [alert show];
            }else{
                UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@""
                                                               message:dic[@"msg"] delegate:self
                                                     cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                [alert show];
            }
            
        } failure:^(NSURLSessionDataTask *operation, NSError *error) {
            NSLog(@"Error: %@", error);
            NSLog(@"请求出错了: - %@ \n%@",
                  [error localizedDescription],
                  [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//键盘谈下去
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_passWordField resignFirstResponder];
    [_telTextField resignFirstResponder];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(alertView.tag==100){
        DingWeiViewController *dingWeiVC=[[DingWeiViewController alloc] init];
        [self.navigationController pushViewController:dingWeiVC animated:YES];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
